package cn.jbit.stream.serialpojo;

import cn.jbit.stream.pojo.User;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

public class ObjectSerialTest {
    /**
     * 对象序列化
     * @throws IOException
     */
    @Test
    public  void  testObjectSerializeable() throws IOException {

        ObjectOutputStream  outputStream=new ObjectOutputStream(new FileOutputStream("D:/User.txt"));

        User user=new User();
        user.setId(1);
        user.setName("哈哈");
        user.setPassword("123.com");

        outputStream.writeObject(user);

        outputStream.close();

    }

    /**
     * 对象反序列化
     */
    @Test
    public  void  objectUnSerializable() throws IOException, ClassNotFoundException {

        ObjectInputStream  inputStream=new ObjectInputStream(new FileInputStream("D:/User.txt"));

        Object o = inputStream.readObject();

        if (o instanceof User){

            User user= (User) o;

            System.out.println(user);

        }

        inputStream.close();

    }


    /**
     * 文件拷贝
     */
    @Test
    public  void testOutPutStreamReader() throws IOException {
        File file=new File("D:\\开源代码网址.txt");
        FileInputStream  fileInputStream=new FileInputStream(file);

        FileOutputStream outputStream=new FileOutputStream(new File("E:\\zaicheng.txt"));

        byte[] bytes=new byte[1024];

        int len=0;

        while ((len=fileInputStream.read(bytes))!=-1){

            outputStream.write(bytes,0,len);

        }
        outputStream.close();
        fileInputStream.close();
    }

    /**
     * 实现字节流与字符流的转换
     */
    @Test
    public  void  testOutPutStream() throws IOException {

        FileInputStream  inputStream=new FileInputStream(new File("D:\\开源代码网址.txt"));

        InputStreamReader  inputStreamReader=new InputStreamReader(inputStream);

        char[] chars=new char[1026];

        int length=-1;

        while ((length=inputStreamReader.read(chars))!=-1){

            System.out.println(chars);

        }
        inputStream.close();
    }


    /**
     * 反射加载配置文件获取文件信息
     */
     @Test
     public  void  testClassLoadInfo() throws IOException {

         //创建配置类对象
         Properties p=new Properties();

         InputStream inputStream = ObjectSerialTest.class.getClassLoader().getResourceAsStream("db.properties");

         InputStreamReader reader=new InputStreamReader(inputStream);

         p.load(reader);

         String s = p.getProperty("pwd");
         System.out.println(s);

     }

    /**
     * 获取文件的列表进行过滤
     */
    @Test
     public  void testListFiles() throws IOException, URISyntaxException {

//         File file1=new File(new URI("D:\\idea2020install\\idea2020workspace\\ReflectProject\\src\\main\\java\\com\\jbit\\reflect"));

         File file=new File("D:\\idea2020install\\idea2020workspace\\ReflectProject\\src\\main\\java\\com\\jbit\\reflect");

         File[] files=file.listFiles(f->f.isDirectory()||f.getName().endsWith(".class"));

         for (File f:files){

             if (f.isFile()){
                 String canonicalPath = f.getCanonicalPath();

                 canonicalPath= canonicalPath.substring(canonicalPath.indexOf("com\\jbit\\reflect"));

                 canonicalPath.replace(".class","").replace("/",".");

                 System.out.println(canonicalPath);

             }else {
                 //testListFiles();
             }
         }
     }
}
